package com.shanghaionstar.navi.search.history;

import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.shanghaionstar.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhaoyang on 2017/7/21.
 */

public class NavSearchHistoryFragment extends BaseFragment {

    RecyclerView mRecommendRv;
    RecyclerView mHistoryRv;

    NavSearchRecommendAdapter mRecommendAdapter;
    private NavSearchHistoryAdapter mHistoryAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_nav_search_history;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initRecommendRv();
        initHistoryRv();
    }

    private void initHistoryRv() {
        List<String> tempData = new ArrayList<>();

        for (int i = 0;i<5;i++){
            tempData.add("hahahaha"+i);
        }

        mHistoryRv = (RecyclerView) getContentView().findViewById(R.id.search_history_rv);
        mHistoryAdapter = new NavSearchHistoryAdapter(getActivity(), tempData);
        mHistoryRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        mHistoryRv.setAdapter(mHistoryAdapter);
    }

    private void initRecommendRv() {
        mRecommendRv = (RecyclerView) getContentView().findViewById(R.id.search_history_recommend_rv);
        mRecommendAdapter = new NavSearchRecommendAdapter(getActivity(), initRecommendData());
        mRecommendRv.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        mRecommendRv.setAdapter(mRecommendAdapter);
    }

    private List<Map<String, Object>> initRecommendData() {
        TypedArray ar = getResources().obtainTypedArray(R.array.gridItem_image);
        int len = ar.length();
        int[] gridViewImages = new int[len];
        for (int i = 0; i < len; i++) {
            gridViewImages[i] = ar.getResourceId(i, 0);
        }
        ar.recycle();
        String[] gridViewTextInfos = getResources().getStringArray(R.array.griditem_info);
        return loadSearchItem(gridViewImages, gridViewTextInfos);
    }

    private List<Map<String, Object>> loadSearchItem(int[] gridViewImages, String[] gridViewTextInfos) {
        List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < gridViewImages.length; i++) {
            Map<String, Object> item = new HashMap<String, Object>();
            item.put("grid_image", gridViewImages[i]);
            item.put("text_item_info", gridViewTextInfos[i]);
            items.add(item);
        }

        return items;
    }
}
